<template>
  <div id="box">
    <div class="write">发布通知</div>
    <div class="content">
      <el-form
        :model="ruleForm"
        :rules="rules"
        ref="ruleForm"
        label-width="100px"
        class="demo-ruleForm"
      >
        <el-form-item label="主题" prop="name">
          <el-input v-model="ruleForm.name"></el-input>
        </el-form-item>
        <el-form-item label="班级" prop="region">
          <el-select v-model="ruleForm.region" placeholder="请选择发送班级">
            <el-option
              v-for="item in options"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="内容" prop="desc">
          <el-input type="textarea" v-model="ruleForm.desc"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitForm('ruleForm')"
            >立即发布</el-button
          >
          <el-button @click="resetForm('ruleForm')">重置</el-button>
        </el-form-item>
      </el-form>
      <div class="result">
        <div class="title">通知记录</div>
        <div class="recording">
          <el-table :data="tableData" stripe style="width: 100%">
            <el-table-column
              prop="date"
              label="发布时间"
              width="300"
            ></el-table-column>
            <el-table-column
              prop="class"
              label="班级"
              width="300"
            ></el-table-column>
            <!-- <el-table-column
              prop="name"
              label="发送人"
              width="250"
            ></el-table-column> -->
            <el-table-column
              prop="title"
              label="标题"
              width="500"
            ></el-table-column>

            <el-table-column fixed="right" label="操作">
              <template slot-scope="scope">
                <el-button @click="read(scope.row)" type="text" size="small"
                  >查看</el-button
                >
                <el-button type="text" size="small" @click="del(scope.row)"
                  >删除</el-button
                >
              </template>
            </el-table-column>
          </el-table>
        </div>
      </div>
    </div>
  </div>
</template>
<script>
export default {
  data() {
    return {
      ruleForm: {
        name: "",
        desc: "",
      },
      rules: {
        name: [{ required: true, message: "请输入通知主题", trigger: "blur" }],
        desc: [{ required: true, message: "请填写通知内容", trigger: "blur" }],
        region: [
          { required: true, message: "请填写通知班级", trigger: "blur" },
        ],
      },
      tableData: [],
      options: [],
      value: "",
    };
  },
  created: function () {
    this.getIndex();
  },
  methods: {
    getIndex() {
      this.$api({
        url: "/index/message_admin",
        method: "get",
        success: (data) => {
          console.log(data);
          data.forEach((value) => {
            var listdata = {
              date: value.create_time.substring(0, 10),
              //   name: value.name,
              title: value.title,
              content: value.content,
              class: value.class,
              id: value.id,
            };
            this.tableData.push(listdata);
            listdata = "";
          });
        },
      });
      this.$api({
        url: "/index/classtype",
        method: "get",
        success: (data) => {
          console.log(data);
          this.options = [];
          data.forEach((value) => {
            var listdata = {
              value: value.class,
              label: value.class + "班",
            };
            this.options.push(listdata);
            listdata = "";
          });
        },
      });
    },
    read(row) {
      this.$alert(row.content, row.title, {
        confirmButtonText: "确定",
        callback: (action) => {
          this.$message({
            type: "info",
            message: `阅读成功！`,
          });
        },
      });
    },
    del(row) {
      console.log(row.id);
      this.$api({
        url: "/index/del_message",
        method: "post",
        data: {
          id: row.id,
        },
        success: (data) => {
          alert("删除成功！");
          this.tableData = [];
          this.getIndex();
        },
      });
    },
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.$api({
            url: "/index/pub_adminmessage",
            method: "post",
            data: {
              count: window.localStorage.getItem("username"),
              title: this.ruleForm.name,
              content: this.ruleForm.desc,
              classno: this.ruleForm.region,
            },
            success: (data) => {
              alert(data);
              (this.ruleForm.name = ""),
                (this.ruleForm.desc = ""),
                (this.ruleForm.region = "");
              this.tableData = [];
              this.getIndex();
            },
          });
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
  },
};
</script>

<style lang="scss" scoped>
#box {
  border: 2px solid #ccc;
  .write {
    font-size: 20px;
    text-align: center;
    height: 50px;
    font-weight: bold;
    line-height: 50px;
    border-bottom: 1px solid #ccc;
  }
  .content {
    padding: 20px;
    margin-top: 25px;

    .el-select {
      padding: 10px 0;
    }
    .block {
      padding: 10px 0;
    }
    .reason {
      padding: 10px 0;
      textarea {
        width: 90%;
        height: 50px;
        outline-style: none;
        resize: none;
        display: block;
        border: 1px solid #dcdfe6;
        padding: 10px;
      }
    }
    .btn {
      padding: 10px 0 30px 0;
      padding-right: 120px;
      display: flex;
      justify-content: flex-end;
      border-bottom: 1px solid #ccc;
      margin-bottom: 30px;
    }
    .result {
      padding-bottom: 50px;
      .title {
        font-size: 20px;
        color: #ccc;
        transition: all 0.5s ease;
        text-align: center;
        padding: 5px;
        &::before {
          content: "";
          display: inline-block;
          height: 2px;
          width: 50px;
          background: #ccc;
          position: relative;
          top: -5px;
          transition: all 0.5s ease;
        }
        &::after {
          content: "";
          display: inline-block;
          height: 2px;
          width: 50px;
          background: #ccc;
          position: relative;
          top: -5px;
          transition: all 0.5s ease;
        }
      }
      &:hover .title {
        color: skyblue;
        transition: all 0.5s ease;
      }
      &:hover .title::before {
        width: 200px;
        background: skyblue;
        transition: all 0.5s ease;
      }
      &:hover .title::after {
        width: 200px;
        background: skyblue;
        transition: all 0.5s ease;
      }
    }
  }
}
</style>
